// import {otherRouter, appRouter} from '@/router/router';
// import Util from '@/libs/util';
// import Cookies from 'js-cookie';
import Vue from 'vue';

const app = {
    state: {
        menuRouterId:"",
        searchResouce:"menu",
        searchData:{},
        workplanSearchDate:"",//调度-工班计划-查询日期
        colorArray:["#1AB667","#00FFFF","#4EFEB3","#FFC78E","#FF5809","#9AFF02","#CCFF80","#36CE57","#E02020","#338CE8","#EFAE12","#FF2D2D","#FF359A","#FF00FF","#00FF7F","#7FFFD4","#FFFF00","#FF6A6A","#FF6347","#FF4500","#FF4040"],
        drawerState:false,
        cachePage: [],
        lang: '',
        websocketData:{},//消息推送的内容
        pushMsgObj:{},//推送过来的消息；
        isFullScreen: false,
        openedSubmenuArr: JSON.parse(sessionStorage.getItem("TagList")), // 要展开的菜单数组
        menuTheme: 'dark', // 主题
        themeColor: {
                    name: '主题三',
                    fontColor: '#495060',
                    bgColor: '#CDD8EA'
                },
        pageOpenedList: [{
            title: '首页',
            path: '',
            name: 'home_index'
        }],
        currentPagePath: '',
        currentPath: [
            {
                title: '首页',
                path: '',
                name: 'home_index'
            }
        ], // 面包屑数组
        menuList: [],
        // routers: [
        //     otherRouter,
        //     ...appRouter
        // ],
        // tagsList: [...otherRouter.children],
        messageCount: 0,
        dontCache: ['text-editor', 'artical-publish'], // 在这里定义你不想要缓存的页面的name属性值(参见路由配置router.js)
        tableList: '',
        historyUrl:[]
    },
    mutations: {
        // 更新历史地址
        updateHistory(state,url){
           if(url =='0')
           {
             state.historyUrl = []
           }else{
              var filterObj = state.historyUrl.filter(item => item.path == url.path)
              if(filterObj.length ==0){
                 state.historyUrl.push(url);
              }
           } 
        },
        // 更新点击状态
        updateSearchResource(state,searchState){
           state.searchResouce = searchState;
        },
        // 更新工班计划查询日期
        updateWorkPlanDate(state,searchDate){
           state.workplanSearchDate = searchDate;
        },
        // 更新tag清单
        updateTagList(state,item){
           // state.openedSubmenuArr = item;
           window.sessionStorage.setItem("TagList",JSON.stringify(item))
        },
        updateWebSocket(state,item){
            // alert(item)
           state.websocketData = item;
        },
        sotreLastedMsg(state,item){
            // alert(item)
           state.pushMsgObj = item;
        },
        updateTheme(state,item){
           state.themeColor = item;
        },
        updateCurrentPath(state,path){
            // alert(state.drawerState)
            state.currentPagePath = path;
            // alert(state.drawerState)
        },
        updateDrawerState(state){
            // alert(state.drawerState)
            state.drawerState = !state.drawerState;
            // alert(state.drawerState)
        },
        setTagsList (state, list) {
            state.tagsList.push(...list);
        },
        updateTableList (state, list) {
            state.tableList = list
        },
        updateMenulist (state) {
            // let accessCode = parseInt(Cookies.get('access'));
            // let menuList = [];
            // appRouter.forEach((item, index) => {
            //     if (item.access !== undefined) {
            //         if (Util.showThisRoute(item.access, accessCode)) {
            //             if (item.children.length === 1) {
            //                 menuList.push(item);
            //             } else {
            //                 let len = menuList.push(item);
            //                 let childrenArr = [];
            //                 childrenArr = item.children.filter(child => {
            //                     if (child.access !== undefined) {
            //                         if (child.access === accessCode) {
            //                             return child;
            //                         }
            //                     } else {
            //                         return child;
            //                     }
            //                 });
            //                 menuList[len - 1].children = childrenArr;
            //             }
            //         }
            //     } else {
            //         if (item.children.length === 1) {
            //             menuList.push(item);
            //         } else {
            //             let len = menuList.push(item);
            //             let childrenArr = [];
            //             childrenArr = item.children.filter(child => {
            //                 if (child.access !== undefined) {
            //                     if (Util.showThisRoute(child.access, accessCode)) {
            //                         return child;
            //                     }
            //                 } else {
            //                     return child;
            //                 }
            //             });
            //             let handledItem = JSON.parse(JSON.stringify(menuList[len - 1]));
            //             handledItem.children = childrenArr;
            //             menuList.splice(len - 1, 1, handledItem);
            //         }
            //     }
            // });
            // state.menuList = menuList;
        },
        changeMenuTheme (state, theme) {
            state.menuTheme = theme;
        },
        changeMainTheme (state, mainTheme) {
            state.themeColor = mainTheme;
        },
        addOpenSubmenu (state, name) {
            let hasThisName = false;
            let isEmpty = false;
            if (name.length === 0) {
                isEmpty = true;
            }
            if (state.openedSubmenuArr.indexOf(name) > -1) {
                hasThisName = true;
            }
            if (!hasThisName && !isEmpty) {
                state.openedSubmenuArr.push(name);
            }
        },
        closePage (state, name) {
            state.cachePage.forEach((item, index) => {
                if (item === name) {
                    state.cachePage.splice(index, 1);
                }
            });
        },
        initCachepage (state) {
            if (localStorage.cachePage) {
                state.cachePage = JSON.parse(localStorage.cachePage);
            }
        },
        removeTag (state, name) {
            state.pageOpenedList.map((item, index) => {
                if (item.name === name) {
                    state.pageOpenedList.splice(index, 1);
                }
            });
        },
        pageOpenedList (state, get) {
            let openedPage = state.pageOpenedList[get.index];
            if (get.argu) {
                openedPage.argu = get.argu;
            }
            if (get.query) {
                openedPage.query = get.query;
            }
            state.pageOpenedList.splice(get.index, 1, openedPage);
            localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList);
        },
        clearAllTags (state) {
            state.pageOpenedList.splice(1);
            state.cachePage.length = 0;
            localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList);
        },
        clearOtherTags (state, vm) {
            let currentName = vm.$route.name;
            let currentIndex = 0;
            state.pageOpenedList.forEach((item, index) => {
                if (item.name === currentName) {
                    currentIndex = index;
                }
            });
            if (currentIndex === 0) {
                state.pageOpenedList.splice(1);
            } else {
                state.pageOpenedList.splice(currentIndex + 1);
                state.pageOpenedList.splice(1, currentIndex - 1);
            }
            let newCachepage = state.cachePage.filter(item => {
                return item === currentName;
            });
            state.cachePage = newCachepage;
            localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList);
        },
        setOpenedList (state) {
            // state.pageOpenedList = localStorage.pageOpenedList ? JSON.parse(localStorage.pageOpenedList) : [otherRouter.children[0]];
        },
        setCurrentPath (state, pathArr) {
            state.currentPath = pathArr;
        },
        setCurrentPageName (state, name) {
            state.currentPageName = name;
        },
        setAvator (state, path) {
            localStorage.avatorImgPath = path;
        },
        switchLang (state, lang) {
            state.lang = lang;
            Vue.config.lang = lang;
        },
        clearOpenedSubmenu (state) {
            state.openedSubmenuArr.length = 0;
        },
        setMessageCount (state, count) {
            state.messageCount = count;
        }
        // increateTag (state, tagObj) {
        //     if (!Util.oneOf(tagObj.name, state.dontCache)) {
        //         state.cachePage.push(tagObj.name);
        //         localStorage.cachePage = JSON.stringify(state.cachePage);
        //     }
        //     state.pageOpenedList.push(tagObj);
        //     localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList);
        // }
    }
};

export default app;
